package GUI;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Desktop.Action;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;

import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;

import Client.ChatClientSendThread;
import Mailbox.Mailbox;
import Mailbox.MailboxFTP;

public class GUIprivate extends JFrame {
	private static final long serialVersionUID = 27L;

	private JPanel chatWindow; // The chat window
	public JTextArea output; // The window where the messages are displayed
	private JTextArea input; // The window where the user writes his message

	private JScrollPane outputPane; // Scroll for the output window
	private JScrollPane inputPane; // Scroll for the input window

	private JButton sendButton; // Sends a message
	private JButton fileButton; // Sends a file
	private JButton exitButton; // Exits the program

	private String username;

	private String title;

	private JFileChooser fileChooser;

	private MailboxFTP mbftp;

	private GUIstartpage sp;

	private static ChatClientSendThread client = null;

	public GUIprivate(ChatClientSendThread client, String username,
			String title, MailboxFTP mbftp, GUIstartpage sp) {
		this.sp = sp;
		this.username = username;

		this.mbftp = mbftp;
		this.title = title;

		this.client = client;

		chatWindow = new JPanel();
		setTitle(username + " | " + title);

		output = new JTextArea();
		output.setEditable(false);
		outputPane = new JScrollPane(output);
		outputPane.setPreferredSize(new Dimension(400, 240));
		outputPane
				.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

		input = new JTextArea("", 5, 36);
		input.setLineWrap(true);
		input.setText("");
		input.addKeyListener(keyListener);
		input.requestFocus();
		inputPane = new JScrollPane(input);

		sendButton = new SendButton("Send message");
		fileButton = new FileButton("Send a file");
		exitButton = new ExitButton("Exit");

		sendButton.setFont(new Font("Arial", Font.BOLD, 16));

		chatWindow.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();

		// The output pane
		c.gridx = 0;
		c.gridy = 0;
		c.gridwidth = 6;
		c.insets = new Insets(10, 0, 10, 10);
		chatWindow.add(outputPane, c);

		// For the input pane
		c = new GridBagConstraints();
		c.gridx = 0;
		c.gridy = 1;
		c.gridwidth = 6;
		c.insets = new Insets(0, 0, 10, 0);
		c.anchor = GridBagConstraints.WEST;
		chatWindow.add(inputPane, c);

		// For the send button
		c.gridx = 0;
		c.gridy = 3;
		c.anchor = GridBagConstraints.LINE_START;
		c.insets = new Insets(0, 0, 10, 0);
		chatWindow.add(sendButton, c);
		c.gridx = 0;
		c.gridy = 4;
		chatWindow.add(fileButton, c);

		// For the startChatbutton
		c.gridx = 5;
		c.gridy = 3;
		c.anchor = GridBagConstraints.LINE_END;
		chatWindow.add(exitButton, c);

		// Adds chatWindow
		add(chatWindow, BorderLayout.CENTER);

		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		setLocation(200, 200);
		setSize(500, 500);
		setVisible(true);

	}

	public static void main(String[] args) {
		// new GUIprivate(client, "Fia", "Gustav", "TestRoom");
	}

	public void setOutput(String message) {
		String oldMessages = output.getText();
		oldMessages = oldMessages.substring(0, oldMessages.length() - 16);
		output.setText(oldMessages + message + "<br /></body>\n</html>");
	}

	public void sendMessage(String message) throws MalformedURLException {
		String temp = message + ":" + title;
		client.send("M " + temp);
	}

	/* Sends a message by pressing Enter */
	KeyListener keyListener = new KeyListener() {
		public void keyPressed(KeyEvent keyEvent) {
			if (keyEvent.getKeyCode() == KeyEvent.VK_ENTER
					&& keyEvent.isControlDown()) {
				input.append("\n");
			} else if (keyEvent.getKeyCode() == KeyEvent.VK_ENTER) {
				sendButton.doClick();
				keyEvent.consume();
			}
		}

		public void keyReleased(KeyEvent keyEvent) {
			if (keyEvent.getKeyCode() == KeyEvent.KEY_RELEASED) {
				output.setText("Has written a text");
			}
		}

		public void keyTyped(KeyEvent keyEvent) {
			if (keyEvent.getKeyCode() == KeyEvent.KEY_PRESSED) {
				output.setText("Is writting a text");
			}
		}
	};

	private class SendButton extends JButton implements ActionListener {

		private static final long serialVersionUID = 27L;

		private SendButton(String name) {
			super(name);
			addActionListener(this);
		}

		@Override
		public void actionPerformed(ActionEvent arg0) {

			if (input.getText() != "") {
				String message = input.getText();
				try {
					sendMessage(message);
				} catch (MalformedURLException e) {
					e.printStackTrace();
				}
			}
		}
	}

	private class FileButton extends JButton implements ActionListener {

		/**
		 * 
		 */
		private static final long serialVersionUID = 27L;

		private FileButton(String name) {
			super(name);
			addActionListener(this);
		}

		@Override
		public void actionPerformed(ActionEvent arg0) {
			fileChooser = new JFileChooser();
			fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
			int returnVal = fileChooser.showOpenDialog(null);
			if (returnVal == JFileChooser.APPROVE_OPTION) {
				File f = fileChooser.getSelectedFile();
				mbftp.saveFile(f);
				client.send("Filetransfer," + title);
			} else {
				System.out.println("No Selection ");
			}
		}
	}

	private class ExitButton extends JButton implements ActionListener {

		/**
		 * 
		 */
		private static final long serialVersionUID = 27L;

		private ExitButton(String name) {
			super(name);
			addActionListener(this);
		}

		@Override
		public void actionPerformed(ActionEvent arg0) {
			client.send("L " + title);
			sp.removeRoomFromLists(title);
			setVisible(false);
			dispose();
		}
	}

	public void writeMessage(String message) {
		String tmp = output.getText();
		output.setText(tmp + input.getText());
		input.setText("");
	}

	public void displayMessage(String message) {
		String tmp = output.getText();
		input.setText("");
		if (!tmp.equals("")) {
			output.setText(tmp + "\n" + message);
			output.setCaretPosition(output.getDocument().getLength());
		} else {
			output.setText(message);
			output.setCaretPosition(output.getDocument().getLength());
		}

	}
}
